package  
{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.ui.*;
	import iso.*;
	
	/**
	 * IsoObject 移动测试
	 * @author StefanieKaka
	 */
	public class MotionTest extends Sprite 
	{
		/// 构造函数
		public function MotionTest() 
		{
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			m_world = new IsoWorld();
			m_world.x = this.stage.stageWidth / 2;
			m_world.y = 100;
			this.addChild(m_world);
			
			for (var i : int = 0; i < 20; ++i)
			{
				for (var j : int = 0; j < 20; ++j)
				{
					var tile : DrawnIsoTile = new DrawnIsoTile(20, 0xcccccc);
					tile.x = i * 20;
					tile.z = j * 20;
					m_world.addChildToFloor(tile);
				}
			}
			
			m_box = new DrawnIsoBox(20, Math.random() * 0xffffff, 20);
			m_box.x = 200;
			m_box.z = 200;
			m_world.addChildToWorld(m_box);
			
			m_shadow = new DrawnIsoTile(20, 0);
			m_shadow.alpha = 0.5;
			m_world.addChildToFloor(m_shadow);
			
			m_filter = new BlurFilter();
			
			this.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
			stage.addEventListener(MouseEvent.CLICK, onClickHandler);
		}
		
		/// 鼠标更新事件
		private function onClickHandler(evt : MouseEvent) : void
		{
			m_box.velocity.x = Math.random() * 20 - 10;
			m_box.velocity.y = -Math.random() * 40;
			m_box.velocity.z = Math.random() * 20 - 10;
		}
		
		/// 更新
		private function onEnterFrameHandler(evt : Event) : void
		{
			m_box.velocity.y += GRAVITY;
			m_box.x += m_box.velocity.x;
			m_box.y += m_box.velocity.y;
			m_box.z += m_box.velocity.z;
			
			if (m_box.x > 380)
			{
				m_box.x = 380;
				m_box.velocity.x *= BOUNCE;
			}
			else if (m_box.x < 0)
			{
				m_box.x = 0;
				m_box.velocity.x *= BOUNCE;
			}
			
			if (m_box.z > 380)
			{
				m_box.z = 380;
				m_box.velocity.z *= BOUNCE;
			}
			else if (m_box.z < 0)
			{
				m_box.z = 0;
				m_box.velocity.z *= BOUNCE;
			}
			
			if (m_box.y > 0)
			{
				m_box.y = 0;
				m_box.velocity.y *= BOUNCE;
			}
			
			m_box.velocity.x *= FRICTION;
			m_box.velocity.y *= FRICTION;
			m_box.velocity.z *= FRICTION;
			
			m_shadow.x = m_box.x;
			m_shadow.z = m_box.z;
			m_filter.blurX = m_filter.blurY = -m_box.y * 0.25;
			m_shadow.filters = [m_filter];
			
			m_world.sort();
		}
		
		private var m_filter	: BlurFilter = null;
		private var m_box		: DrawnIsoBox = null;
		private var m_shadow	: DrawnIsoTile = null;
		private var m_world		: IsoWorld = null;
		
		private static const SPEED		: Number = 5;
		private static const GRAVITY	: Number = 2;
		private static const FRICTION	: Number = 0.95;
		private static const BOUNCE		: Number = -0.9;
	}

}